package jtacck.labmedicine;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import jtacck.labmedicine.Cache.ChecklistState;
import jvm.jtacck.db.records.ClientSite;
import jvm.jtacck.db.records.ShippingInformation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Listens for an item to be selected on a ListView in CheckListActivity.
 * 
 * @author Kevin
 * 
 */
public class ChecklistListener implements OnClickListener {
    private Activity current;
    private Class<?> next;
    private ChecklistState state;
    private Serializable data;

    /**
     * @param current
     *            The Activity that uses this object.
     * @param next
     *            The next Activity to start when onClick is called.
     * @param data
     *            Data to be sent to the server when onClick is called.
     */
    protected ChecklistListener(Activity current, Class<?> next,
            ChecklistState state, Serializable data) {
        this.current = current;
        this.next = next;
        this.state = state;
        this.data = data;
    }

    /**
     * Makes sure all items are checked then if they are starts the next
     * activity. If some item is not checked then a message is displayed
     * informing the user of that fact.
     * 
     * @param view
     *            This is the object that was clicked.
     * 
     * @Override
     */
    public void onClick(View view) {
        ListView listView = (ListView) current
                .findViewById(R.id.instruction_view);

        boolean allChecked = true;
        for (int i = 0; i < listView.getCount(); i++) {
            allChecked = allChecked && listView.isItemChecked(i);
        }

        if (allChecked) {
            if (next != null) {
                Intent intent = new Intent(current.getApplicationContext(),
                        next);
                if (next.equals(RouteMapTabActivity.class)
                        && state.equals(Cache.ChecklistState.SHIPPING)) {
                    if (!RouteListActivity.end.isEmpty()) {
                        RouteListActivity.current.add(RouteListActivity.end
                                .get(0));
                        RouteListActivity.end.remove(0);
                    }
                    RouteListActivity.beginning.add(RouteListActivity.current
                            .get(0));
                    RouteListActivity.current.remove(0);
                }

                // Test to see if this is a ClientSite checklist. If it is then
                // it
                // links to a package list checklist and extra steps must be
                // taken
                // to initialize the next Activity.
                if (next.equals(ChecklistActivity.class)) {
                    String[] toDo = new String[] { "Error" };

                    // if (data instanceof ClientSite) {
                    // MainActivity.sList = MainActivity.linker
                    // .getShippingList((ClientSite) data);

                    // if (MainActivity.sList != null) {
                    // List<ShippingInformation> list =
                    // MainActivity.sList.shippingList;
                    // List<String> strings = new LinkedList<String>();
                    //
                    // for (int i = 0; i < list.size(); i++) {
                    // if (list.get(i).status.status_id == 1) {
                    // strings.add(list.get(i).specimen.description);
                    // }
                    // }
                    // toDo = strings.toArray(new String[0]);
                    // }

                    // }

                    Bundle extras = new Bundle();

                    switch (state) {
                    case SITE:
                        extras.putString("state", "SHIPPING");
                        extras.putBoolean("freeform", false);
                        break;
                    case SHIPPING:
                        extras.putString("string", "END");
                    default:
                        extras.putString("state", "START");
                    }

                    // extras.putStringArray("toDo", toDo);
                    extras.putSerializable("nextActivity",
                            RouteMapTabActivity.class);
                    // extras.putBoolean("conditional", true);
                    extras.putSerializable("forResult", PackageActivity.class);

                    intent.putExtra("jtacck", extras);
                }
                current.startActivity(intent);
                current.finish();
            } else
                SharedMethods.logoutAlert(current).show();
        } else {
            Toast.makeText(current.getApplicationContext(),
                    "YOU DIDN'T COMPLETE THE CHECKLIST", Toast.LENGTH_LONG)
                    .show();
            return;
        }
    }

}
